package com.flashmoment.video.filter;

import com.alibaba.fastjson.JSON;
import com.flashmoment.video.utils.JwtUtils;

import com.flashmoment.video.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Title: LoginCheckFilter
 * @Author xulu
 * @Package com.flashmoment.video.filter
 * @Date 2023/10/28 17:51
 */
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {


    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        String token = request.getHeader("token");

        log.info("拦截到请求:{}", requestURI);


        //1.不进行拦截的路径
        String[] urls = new String[]{
                "/video/usercomment/findCommentByVideoId/**",
                "/video/user/findMyLikesByUserId/**",
                "/video/user/findMyCollectByUserId/**",
                "/video/user/findMyConcernByUserId/**",
                "/video/usercomment/list",    //获取评论
                "/video/usercomment/save/**",    //评论
                "/video/usercollect/save",       //收藏
                "/video/usercollect/update",       //收藏
                "/video/usercollect/delete",
                "/video/usercollect/deleteByVideoId/",
                "/video/deleteByVideoId/**",
                "/video/usercollect/deleteCollect",
                "/video/usercomment/findCommentByVideoId/",
                "/video/**",
                "/video/usercollect/update",
                "/video/user/update",
                "/video/delete",
                "/video/user/sendCode",  //发送验证码
                "/video/user/login",     //登录
                "/video/user/register", //注册
                "/video/user/forgetPassword", //忘记密码
                "/video/video/list",    //视频列表
                "/video/video/recommend", //视频推荐
                "/video/videotype/list",  //视频类型列表
                "/video/video/findVideoById/{id}" ,  //视频类型详情
                "/video/video/findVideoData/{pageNumber}/{pageSize}/{keywords}", //高亮搜索
                "/doc.html",
                "/favicon.ico",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs",
                "/mode-Text.js",
                "/video/updata/uploadFile",
                "/video/updata/upload",
                "/swagger-ui.html"

        };

        //2.匹配路径
        boolean check = check(urls, requestURI);
        //3.如果不需要处理,则直接放行
        if (check) {
            log.info("本次请求不需要处理:{}", requestURI);
            filterChain.doFilter(request, response);
            return;
        }
        //4.判断token是否有效
        if (token != null && JwtUtils.checkToken(token)) {
            log.info("token有效");
            log.info("token："+token);
            filterChain.doFilter(request, response);
            return;
        }else{
            log.info("token失效");
        }

        //5.如果token无效则返回对应的信息
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN!")));
        return;

    }

    public boolean check(String[] urls, String requestURL) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURL);
            if (match) {
                return true;
            }
        }
        return false;
    }

}
